package com.hxkj.config;

import com.hxkj.client.service.taglib.directive.*;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.TemplateExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.PostConstruct;
import java.util.Locale;
import java.util.Properties;

import static com.hxkj.client.service.taglib.config.LableConstant.*;

/**
 * Created by yaosy on 2018年6月26日19:10:12
 */
@Configuration
public class FreeMarkerConfig {
    @Value("${cms.realPath}")
    private String Path;

    @Value("${cms.pathUrl}")
    private String PathUrl;

    @Value("${cms.tpl}")
    private String Tpl;

    @Value("${urlapi.urlPort}")
    private String urlPort;

    @Value("${urlapi.ucUrl}")
    private String ucUrl;

    @Value("${cms.tmpRes}")
    private String tmpRes;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;


    @Autowired
    protected FreeMarkerProperties properties;


    @Autowired
    private BreadcrumbsDirective breadcrumbsDirective;

    @Autowired
    private NavigationDirective navigationDirective;

    @Autowired
    private PreAndNextArticleDirective preAndNextArticleDirective;

    @Autowired
    private CorrelationArticleDirective correlationArticleDirective;

    @Autowired
    private ColumnTreeMapDirective columnTreeMapDirective;

    @Autowired
    private HotArticleDirective hotArticleDirective;

    @Autowired
    private TagsDirective tagsDirective;

    @Autowired
    private ColumnDirective columnDirective;

    @Autowired
    private TagArticleDirective tagArticleDirective;

    @Autowired
    private LeafletDirective leafletDirective;

    @Autowired
    private SiteInfoDirective siteInfoDirective;

    @Autowired
    private RightsCountDirective rightsCountDirective;

    @Autowired
    private LeafletDetailDirective leafletDetailDirective;

    @Autowired
    private ScriptResourcesDirective scriptResourcesDirective;

    @Autowired
    private ColumnTreeDirective columnTreeDirective;

    @Autowired
    private ArticleListDirective articleListDirective;

    @Autowired
    private PageInfoDirective pageInfoDirective;

    @Autowired
    private HotArticleTagsDirective hotArticleTagsDirective;

    @Autowired
    private HotTagsDirective hotTagsDirective;

    @Autowired
    private TagDirective tagDirective;

    @Autowired
    private LinkResourcesDirective linkResourcesDirective;

    @Autowired
    private ImgResourcesDirective imgResourcesDirective;

    @Autowired
    private FriendlyLinkDirective friendlyLinkDirective;

    @Autowired
    private NewArticleDirective newArticleDirective;

    @Autowired
    private ColumnsArticleDirective columnsArticleDirective;

    @Autowired
    private RecommendationArticleDirective recommendationArticleDirective;

    @Autowired
    private ColumnArticleListDirective columnArticleListDirective;

    @Autowired
    private AppDownloadDirective appDownloadDirective;

    @Autowired
    private OverallHotArticleDirective overallHotArticleDirective;

    @Autowired
    private OverallSiteDirective overallSiteDirective;

    @Autowired
    private OverallNewestArticleDirective overallNewestArticleDirective;

    @Autowired
    private ColumnPageArticleListDirective columnPageArticleListDirective;

    @Bean
    public FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean() {
        FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
        freeMarkerConfigurationFactoryBean.setPreferFileSystemAccess(true);
        return freeMarkerConfigurationFactoryBean;
    }

    @PostConstruct
    public freemarker.template.Configuration getConfigure() throws Exception {
        freemarker.template.Configuration configuration = freeMarkerConfigurer.getConfiguration();

        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);

        // 设置编码
        configuration.setEncoding(Locale.CHINA, "UTF-8");
        // 版本号
        configuration.setObjectWrapper(new DefaultObjectWrapper(freemarker.template.Configuration.VERSION_2_3_19));
        // 加载html的资源路径
        configuration.setSharedVariable("ctx", "");
        //设置邮箱@符号
        configuration.setSharedVariable("symbol","@");
        // 静态资源文件绝对路径
        configuration.setSharedVariable("PATH", Path + PathUrl);
        // 动态接口ip
        configuration.setSharedVariable("URL", urlPort);
        // 前台用户中心URL
        configuration.setSharedVariable("UCURL", ucUrl);
        // 图片资源路径
        configuration.setSharedVariable("ASSET", Path);
        // 模板资源路径
        configuration.setSharedVariable("TPL", Path + Tpl);
        // 面包屑
        configuration.setSharedVariable(CMS_BREADCRUMBS, breadcrumbsDirective);
        // 导航栏
        configuration.setSharedVariable(CMS_NAVIGATION, navigationDirective);
        // 上一篇 下一篇
        configuration.setSharedVariable(CMS_PREANDNEXTARTICLE, preAndNextArticleDirective);
        // 相关文章推荐
        configuration.setSharedVariable(CMS_CORRELATIONARTICLE, correlationArticleDirective);
        // 单页
        configuration.setSharedVariable(CMS_LEAFLETS, leafletDirective);
        // 网站信息
        configuration.setSharedVariable(CMS_SITEINFO, siteInfoDirective);
        // 咨询和曝光的数量
        configuration.setSharedVariable(CMS_RIGHTSCOUNT, rightsCountDirective);
        // 获取单条单页详情
        configuration.setSharedVariable(CMS_LEAFLETDETAIL, leafletDetailDirective);
        // 获取多条友情链接
        configuration.setSharedVariable(CMS_FRIENDLYLINK, friendlyLinkDirective);
        // 最新文章
        configuration.setSharedVariable(CMS_NEWARTICLE, newArticleDirective);
        // 根据tags的相关推荐文章
        configuration.setSharedVariable(CMS_RECOMMENDATIONARTICLE, recommendationArticleDirective);
        // 分类树
        configuration.setSharedVariable(CMS_COLUMN_TREE, columnTreeMapDirective);
        // 热门文章
        configuration.setSharedVariable(CMS_ARTICLE_HOT, hotArticleDirective);
        // 热门标签
        configuration.setSharedVariable(CMS_TAG_HOT, tagsDirective);
        // 栏目
        configuration.setSharedVariable(CMS_COLUMN, columnDirective);
        // 文章列表标签
        configuration.setSharedVariable(CMS_TAG_ARTICLE, tagArticleDirective);
        // script资源文件路径
        configuration.setSharedVariable(CMS_SCRIPT_RESOURCES_PATH, scriptResourcesDirective);
        // 栏目
        configuration.setSharedVariable(CMS_COLUMN_ID, columnTreeDirective);
        // 文章列表
        configuration.setSharedVariable(CMS_ARTICLE_LIST, articleListDirective);
        // 分页
        configuration.setSharedVariable(CMS_PAGE_INFO, pageInfoDirective);
        // 热门文章标签
        configuration.setSharedVariable(CMS_HOT_ARTICLE_TAGS, hotArticleTagsDirective);
        // 热门标签
        configuration.setSharedVariable(CMS_HOT_TAGS, hotTagsDirective);
        // 标签
        configuration.setSharedVariable(CMS_TAG, tagDirective);
        // link资源文件路径
        configuration.setSharedVariable(CMS_LINK_RESOURCES_PATH, linkResourcesDirective);
        // mg资源文件路径
        configuration.setSharedVariable(CMS_IMG_RESOURCES_PATH, imgResourcesDirective);
        // 多个分类文章列表的标签名称
        configuration.setSharedVariable(CMS_COLUMN_ARTICLE, columnsArticleDirective);
        // 单个分类文章列表的标签名称
        configuration.setSharedVariable(CMS_COLUMN_ARTICLELIST, columnArticleListDirective);
        // 软件下载,根据文章ids获取多篇文章,添加了文章的扩展属性
        configuration.setSharedVariable(CMS_APP_DOWNLOAD, appDownloadDirective);
        //Mr.zheng:分类下分页文章列表
        configuration.setSharedVariable(CMS_COLUMN_PAGE_ARTICLE_LIST, columnPageArticleListDirective);

        //----------移动端标签----------
        // 面包屑
        configuration.setSharedVariable(M_CMS_BREADCRUMBS, breadcrumbsDirective);
        // 导航栏
        configuration.setSharedVariable(M_CMS_NAVIGATION, navigationDirective);
        // 上一篇 下一篇
        configuration.setSharedVariable(M_CMS_PREANDNEXTARTICLE, preAndNextArticleDirective);
        // 相关文章推荐
        configuration.setSharedVariable(M_CMS_CORRELATIONARTICLE, correlationArticleDirective);
        // 单页
        configuration.setSharedVariable(M_CMS_LEAFLETS, leafletDirective);
        // 网站信息
        configuration.setSharedVariable(M_CMS_SITEINFO, siteInfoDirective);
        // 咨询和曝光的数量
        configuration.setSharedVariable(M_CMS_RIGHTSCOUNT, rightsCountDirective);
        // 获取单条单页详情
        configuration.setSharedVariable(M_CMS_LEAFLETDETAIL, leafletDetailDirective);
        // 获取多条友情链接
        configuration.setSharedVariable(M_CMS_FRIENDLYLINK, friendlyLinkDirective);
        // 最新文章
        configuration.setSharedVariable(M_CMS_NEWARTICLE, newArticleDirective);
        // 根据tags的相关推荐文章
        configuration.setSharedVariable(M_CMS_RECOMMENDATIONARTICLE, recommendationArticleDirective);
        // 分类树
        configuration.setSharedVariable(M_CMS_COLUMN_TREE, columnTreeMapDirective);
        // 热门文章
        configuration.setSharedVariable(M_CMS_ARTICLE_HOT, hotArticleDirective);
        // 热门标签
        configuration.setSharedVariable(M_CMS_TAG_HOT, tagsDirective);
        // 栏目
        configuration.setSharedVariable(M_CMS_COLUMN, columnDirective);
        // 文章列表标签
        configuration.setSharedVariable(M_CMS_TAG_ARTICLE, tagArticleDirective);
        // script资源文件路径
        configuration.setSharedVariable(M_CMS_SCRIPT_RESOURCES_PATH, scriptResourcesDirective);
        // 栏目
        configuration.setSharedVariable(M_CMS_COLUMN_ID, columnTreeDirective);
        // 文章列表
        configuration.setSharedVariable(M_CMS_ARTICLE_LIST, articleListDirective);
        // 分页
        configuration.setSharedVariable(M_CMS_PAGE_INFO, pageInfoDirective);
        // 热门文章标签
        configuration.setSharedVariable(M_CMS_HOT_ARTICLE_TAGS, hotArticleTagsDirective);
        // 热门标签
        configuration.setSharedVariable(M_CMS_HOT_TAGS, hotTagsDirective);
        // 标签
        configuration.setSharedVariable(M_CMS_TAG, tagDirective);
        // link资源文件路径
        configuration.setSharedVariable(M_CMS_LINK_RESOURCES_PATH, linkResourcesDirective);
        // mg资源文件路径
        configuration.setSharedVariable(M_CMS_IMG_RESOURCES_PATH, imgResourcesDirective);
        // 多个分类文章列表的标签名称
        configuration.setSharedVariable(M_CMS_COLUMN_ARTICLE, columnsArticleDirective);
        // 单个分类文章列表的标签名称
        configuration.setSharedVariable(M_CMS_COLUMN_ARTICLELIST, columnArticleListDirective);
        // 软件下载,根据文章ids获取多篇文章,添加了文章的扩展属性
        configuration.setSharedVariable(M_CMS_APP_DOWNLOAD, appDownloadDirective);
        //查询全站 站点列表
        configuration.setSharedVariable(M_CMS_OVERALL_SITE, overallSiteDirective);
        //查询全站 热门文章
        configuration.setSharedVariable(M_CMS_OVERALL_HOT_ARTICLE, overallHotArticleDirective);
        //查询全站 最新资讯
        configuration.setSharedVariable(M_CMS_OVERALL_NEWEST_ARTICLE, overallNewestArticleDirective);
        //分类下分页文章列表
        configuration.setSharedVariable(M_CMS_COLUMN_PAGE_ARTICLE_LIST, columnPageArticleListDirective);

        return configuration;
    }

    @Bean
    @ConditionalOnMissingBean({ FreeMarkerConfig.class })
    public FreeMarkerConfigurer freemarkerConfig() {
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        applyProperties(configurer);
        return configurer;
    }


    @Bean
    public Properties properties(){
        Properties properties = new Properties();
        // 模板更新时间
        properties.put("template_update_delay",3600);
        properties.put("default_encoding","UTF-8");
        properties.put("output_encoding","UTF-8");
        properties.put("locale","UTF-8");
        properties.put("classic_compatible",true);
        properties.put("datetime_format","yyyy-MM-dd HH:mm:ss");
        properties.put("date_format","yyyy-MM-dd");
        properties.put("date_as_format","MM-dd");
        return properties;
    }


    protected void applyProperties(FreeMarkerConfigurationFactory factory) {
        factory.setTemplateLoaderPaths(this.properties.getTemplateLoaderPath());
        factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
        factory.setDefaultEncoding("UTF-8");
        Properties settings = new Properties();
        settings.putAll(this.properties());
        factory.setFreemarkerSettings(settings);
    }

}
